"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 系统操作-explain
Case Name   : 在insert语句中，使用explain 语法依次添加DETAIL，NODES，NUM_NODES，
              目前版本支持DETAIL参数，NODES，NUM_NODES参数合理报错
Create At   : 2020/12/2
Owner       : z@wan005
Description :
        1.建表
        2.explain添加detail参数
        3.explain添加nodes选项
        4.添加nodes_num选项
        5.删表
Expect      :
        1.建表成功
        2.合理报错，此参数仅分布式模式可用，集中式模式不可用
        3.合理报错，此参数仅分布式模式可用，集中式模式不可用
        4.合理报错，此参数仅分布式模式可用，集中式模式不可用
        5.删表成功
History     :
        modified by @haomeng802 at 2022/1/30:优化用例断言解决参数不支持集中式问题
        modified by @wan005 at 2022.05.20:优化用例，删除敏感信息
"""
import unittest
from testcase.utils.Logger import Logger
from testcase.utils.Constant import Constant
from testcase.utils.CommonSH import CommonSH


logger = Logger()
commonsh = CommonSH('dbuser')
constant = Constant()


class SYS_Operation(unittest.TestCase):
    def setUp(self):
        logger.info('------Opengauss_Function_DML_Set_Case0151开始执行------')
        self.Constant = Constant()
        self.tb_name = 't_explain_0151'

    def test_explain(self):
        text = '---step1:创建测试表   expect:成功---'
        logger.info(text)
        sql_cmd1 = commonsh.execut_db_sql(
            f'''drop table if exists {self.tb_name};
            create table {self.tb_name}(id int, name char(20));''')
        logger.info(sql_cmd1)
        self.assertIn(self.Constant.TABLE_CREATE_SUCCESS, sql_cmd1,
                      '执行失败' + text)

        error_msg = ['ERROR:  unrecognized EXPLAIN option "detail"',
                     'ERROR:  unrecognized EXPLAIN option "nodes"',
                     'ERROR:  unrecognized EXPLAIN option "num_nodes"']

        text = '---step2:explain添加detail参数   expect:合理报错---'
        logger.info(text)
        sql_cmd2 = commonsh.execut_db_sql(f'''
            explain (analyze,detail)insert into {self.tb_name} 
            values(5,'a'),(6,'b');
            explain (analyze,detail true)insert into {self.tb_name} 
            values(5,'a'),(6,'b');
            explain (analyze,detail false)insert into {self.tb_name} 
            values(5,'a'),(6,'b');''')
        logger.info(sql_cmd2)
        self.assertTrue(sql_cmd2.count(error_msg[0]) == 3, '执行失败' + text)

        text = '---step3:explain添加nodes选项   expect:合理报错---'
        logger.info(text)
        sql_cmd3 = commonsh.execut_db_sql(f'''
            explain (analyze,nodes)insert into {self.tb_name} 
            values(5,'a'),(6,'b');
            explain (analyze,nodes true)insert into {self.tb_name} 
            values(5,'a'),(6,'b');
            explain (analyze,nodes false)insert into {self.tb_name} 
            values(5,'a'),(6,'b');''')
        logger.info(sql_cmd3)
        self.assertTrue(sql_cmd3.count(error_msg[1]) == 3, '执行失败' + text)

        text = '---step4:explain添加node_num选项   expect:合理报错---'
        logger.info(text)
        sql_cmd4 = commonsh.execut_db_sql(f'''
            explain (analyze,num_nodes)insert into {self.tb_name} 
            values(5,'a'),(6,'b');
            explain (analyze,num_nodes true)insert into {self.tb_name} 
            values(5,'a'),(6,'b');
            explain (analyze,num_nodes false)insert into {self.tb_name} 
            values(5,'a'),(6,'b');''')
        logger.info(sql_cmd4)
        self.assertTrue(sql_cmd4.count(error_msg[2]) == 3, '执行失败' + text)

    def tearDown(self):
        text = '---step5:清理环境   expect:清理成功---'
        logger.info(text)
        sql_cmd5 = commonsh.execut_db_sql(f'''drop table {self.tb_name};''')
        logger.info(sql_cmd5)
        self.assertIn(self.Constant.DROP_TABLE_SUCCESS, sql_cmd5,
                      '执行失败' + text)
        logger.info('------Opengauss_Function_DML_Set_Case0151执行结束------')
